//获取应用实例
const bgMusic = wx.getBackgroundAudioManager();
const app = getApp();
const recorderManager = wx.getRecorderManager()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    countDownShow: true,
    count: 0,
    lyricShow: false,
    starttime: "00:00", //正在播放时长
    duration: "06:41", //总时长

    openRecordingdis: "block",//录音图片的不同
    shutRecordingdis: "none",//录音图片的不同
    recordingTimeqwe:0,//录音计时
    setInter:""//录音名称
    
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //屏蔽右上角分享功能
    wx.hideShareMenu();

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    this.shutRecording();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {},

  //录音计时器
  recordingTimer:function(){
      var that = this;
    //将计时器赋值给setInter
      that.data.setInter = setInterval(
        function () {
        var time = that.data.recordingTimeqwe + 1;
        console.log(time);
        that.setData({
          recordingTimeqwe: time,
          duration: time
        })
        }
        , 1000); 
  },
    
    
    //开始录音
    openRecording: function() {
      var that = this;
      wx.getSystemInfo({
        success: function(res) {
          that.setData({
            shutRecordingdis: "block",
            openRecordingdis: "none"
          })
        }
      })
      const options = {
        duration: 60000, //指定录音的时长，单位 ms，最大为10分钟（600000），默认为1分钟（60000）
        sampleRate: 16000, //采样率
        numberOfChannels: 1, //录音通道数
        encodeBitRate: 96000, //编码码率
        format: 'mp3', //音频格式，有效值 aac/mp3
        frameSize: 50, //指定帧大小，单位 KB
      }
      //TODO 需要倒计时三秒开始
      that.data.three = setInterval(
        function () {
          var time = that.data.count + 1;
          console.log(time);
          that.setData({
            count: time
          })
          if(time > 3){
            
            //开始录音计时   
            that.recordingTimer();
            //开始录音
            recorderManager.start(options);
            recorderManager.onStart(() => {
              console.log('。。。开始录音。。。')
            });
            //错误回调
            recorderManager.onError((res) => {
              console.log(res);

            })
            clearInterval(that.data.three);
          }
        }
        , 1000); 
    
    },
  
    //结束录音
    shutRecording: function() {
      var that = this;
      wx.getSystemInfo({
        success: function(res) {
          that.setData({
            shutRecordingdis: "none",
            openRecordingdis: "block"
          })
        }
      })
      recorderManager.stop();
      recorderManager.onStop((res) => {
        console.log('。。停止录音。。', res.tempFilePath)
        const {tempFilePath} = res;
        //结束录音计时  
        clearInterval(that.data.setInter);
        //上传录音
        var objectid = "";
        wx.uploadFile({
          url: 'https://wxgt.chaoxing.com/wechat/uploadfile',//服务器接口
          filePath: tempFilePath,
          name:"file",//后台要绑定的名称
          header: {
            "Content-Type": "multipart/form-data"
          },
          success:function(res){
            console.log(res);

            var data = JSON.parse(res.data);
            if(data.code == 200){
              console.log('上传成功');
              objectid = data.data.objectid;
              console.log("objectid: " + objectid);
            }else{
              console.log('接口调用失败');             
            }
            wx.showToast({
              title: '保存完成',
              icon:'success',
              duration:2000
            })
            //TODO 跳转到音频操作页面
            wx.navigateTo({
              url: '/pages/readingAloud/readingAloud?objectid=' + objectid + "&temp=" + tempFilePath,
            })
          },
          fail: function(ress){
            console.log("。。录音保存失败。。");
          }
        })      
    })
  },

  // 暂停录音
  pause: function() {
    var that = this;
    recorderManager.pause()
    recorderManager.onPause((res) => {
      console.log(res)
      console.log('暂停录音')
    
    })
  },

  //继续录音
  continue: function() {
    var that = this;
    console.log("继续录音");
    recorderManager.resume()
    //开始计时
    that.data.three = setInterval(
      function () {
        var time = that.data.count + 1;
        console.log(time);
        that.setData({
          count: time
        })
      }
      , 1000); 
    recorderManager.onResume((res) => {
    })
  },

  // 倒计时
  // Countdown: function(that) {
  //   timer = setTimeout(function() {
  //     console.log("----secondes----" + formatSeconds(secondes));
  //     secondes++;
  //     if (secondes >= 600) {
  //       recorderManager.stop();
  //       clearTimeout(timer);
  //     }
  //     that.setData({
  //       times: formatSeconds(secondes)
  //     });
  //     Countdown(that);
  //   }, 1000)
  // }

  // that.data.three = setInterval(
  //   function () {
  //     var time = that.data.count + 1;
  //     console.log(time);
  //     that.setData({
  //       count: time
  //     })

  
 

});
